﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.OleDb;
using System.Data;
using DTO;

namespace DAO
{
    public class ToCongDoanDAO
    {
        public static List<ToCongDoanDTO> LayDanhSach()
        {

            List<ToCongDoanDTO> lstToCongDoan = new List<ToCongDoanDTO>();
            OleDbConnection cnn = BoKetNoiCSDL.TaoKetNoi();
            string query = "Select * From To_Cong_Doan";
            OleDbCommand oleDbCmd = new OleDbCommand(query, cnn);
            cnn.Open();
            try
            {
                OleDbDataReader rd = oleDbCmd.ExecuteReader();
                while (rd.Read())
                {
                    ToCongDoanDTO toCongDoan = new ToCongDoanDTO();
                    toCongDoan.MaToCD = (int)rd["ToCongDoanID"];
                    toCongDoan.TenToCongDoan = (string)rd["TenToCongDoan"];
                    lstToCongDoan.Add(toCongDoan);
                }

            }
            catch
            {
                if (cnn.State == ConnectionState.Open)
                    cnn.Close();
                throw new Exception("thao tác lấy danh sách tổ công đoàn bị lỗi");
            }
            finally
            {
                cnn.Dispose();
            }

            return lstToCongDoan;
        }

        public static int Them(ToCongDoanDTO toCongDoanDto)
        {
            OleDbConnection cnn = BoKetNoiCSDL.TaoKetNoi();
            string cmdText = String.Format("Insert into To_Cong_Doan(TenToCongDoan) values('{0}')", toCongDoanDto.TenToCongDoan);
            OleDbCommand oleDbCmd = new OleDbCommand(cmdText, cnn);
            cnn.Open();
            try
            {
                return oleDbCmd.ExecuteNonQuery();
            }
            catch
            {
                if (cnn.State == ConnectionState.Open)
                    cnn.Close();
                throw new Exception("thao tác thêm tổ công đoàn bị lỗi");
            }
            finally
            {
                cnn.Dispose();
            }
        }

        public static int Xoa(string tenToCongDoan)
        {
            OleDbConnection cnn = BoKetNoiCSDL.TaoKetNoi();
            string cmdText = String.Format("DELETE FROM To_Cong_Doan WHERE TenToCongDoan = '{0}'", tenToCongDoan);
            OleDbCommand oleDbCmd = new OleDbCommand(cmdText, cnn);
            cnn.Open();
            try
            {
                return oleDbCmd.ExecuteNonQuery();
            }
            catch
            {
                if (cnn.State == ConnectionState.Open)
                    cnn.Close();
                throw new Exception("thao tác xóa tổ công đoàn bị lỗi");
            }
            finally
            {
                cnn.Dispose();
            }
        }

        public static int Sua(string tenTCDCu, string tenTCDMoi)
        {
            OleDbConnection cnn = BoKetNoiCSDL.TaoKetNoi();
            string cmdText = String.Format("UPDATE To_Cong_Doan SET TenToCongDoan = '{0}' WHERE TenToCongDoan = '{1}'", tenTCDMoi, tenTCDCu);
            OleDbCommand oleDbCmd = new OleDbCommand(cmdText, cnn);
            cnn.Open();
            try
            {
                return oleDbCmd.ExecuteNonQuery();
            }
            catch
            {
                if (cnn.State == ConnectionState.Open)
                    cnn.Close();
                throw new Exception("thao tác sửa tổ công đoàn bị lỗi");
            }
            finally
            {
                cnn.Dispose();
            }
        }
    }
}
